package com.hospital.hospitalserver.service;

import com.hospital.hospitalserver.entity.ManagerDetailDTO;
import com.hospital.hospitalserver.entity.UserInfo;
import com.hospital.hospitalserver.entity.Vo.Manager;
import com.hospital.hospitalserver.entity.query.AdminStaffQuery;
import com.hospital.hospitalserver.mapper.ManagerMapper;
import com.hospital.hospitalserver.mapper.UserMapper;
import com.hospital.hospitalserver.util.EncryptHelper;
import com.hospital.hospitalserver.util.PagerData;
import com.hospital.hospitalserver.util.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 行政管理人员实现类
 * */
@Service
@RequiredArgsConstructor
public class ManagerService {
    private final ManagerMapper managerMapper;
    private final UserMapper userMapper;

    /**
     * 新增行政人员
     * 添加事务处理，执行有错误回滚操作
     * */
    @Transactional(rollbackFor = Exception.class)
    public Response addManager(ManagerDetailDTO manager){
        // 1. 判断添加的账户是否存在重复
        UserInfo u = userMapper.findUserByUsername(manager);
        if(u!=null){
            return Response.error("账户名已存在");
        }

        // 2. 设置默认角色
        // 注意这里很重要 role_id = 3 是系统管理员，role_id = 6 行政管理
        if("system_administrator".equals(manager.getStaff_type())){
            manager.setRole_id(3);
        }else{
            manager.setRole_id(6);
        }
        manager.setIs_active(1); // 默认激活
        //系统密码加密
        String encryptedPassword = EncryptHelper.SHA(manager.getUserpass());
        manager.setUserpass(encryptedPassword);

        // 3. 添加用户，此时 manager_id 对象的 user_id 会被自动填充
        Integer addCount = userMapper.addUser(manager);
        if(addCount==0){
            return Response.error("用户添加失败");
        }
        // 4. 此时 manager.getUser_id() 已经包含新生成的 user_id
        manager.setManager_id(manager.getUser_id());

        // 5. 添加行政管理人员详细信息
        Integer count = managerMapper.addManager(manager);
        if(count>0){
            return Response.success();
        }else{
            return Response.error("行政人员信息添加失败");
        }
    }

    /**
     * 编辑行政人员
     * */
    @Transactional(rollbackFor = Exception.class)
    public Response editManager(ManagerDetailDTO manager){
        Integer count = managerMapper.editManager(manager);
        if(count>0){
            return Response.success();
        }else{
            return Response.error();
        }
    }

    /**
     * 删除行政人员
     * */
    public Response deleteManager(ManagerDetailDTO manager){
        Integer count = managerMapper.deleteManager(manager);
        if(count>0){
            return Response.success();
        }else{
            return Response.error();
        }
    }

    /**
     * 加载行政人员职称列表数据
     * */
    public Response getAdminStaffProfessionalList(){
        return Response.success(managerMapper.getAdminStaffProfessionalList());
    }

    /**
     * 加载行政人员分页总数
     * */
    private Integer getAdminStaffCountBySearch(AdminStaffQuery query){
        return managerMapper.getAdminStaffCountBySearch(query);
    }

    /**
     * 计算分页
     * */
    private Integer getAdminStaffPageTotalBySearch(AdminStaffQuery query){
        Integer adminStaffCounts = getAdminStaffCountBySearch(query);
        Integer pageTotal = 1;//设置初始页码为1
        //数据总条数除以pageSize每页显示条数，如取余数求模，如果有余数，页码+1
        //如果可以整除，则页码等于数据总数除以pageSize
        pageTotal = adminStaffCounts/query.getPageSize();
        if(adminStaffCounts%query.getPageSize()>0){
            pageTotal += 1;
        }
        return pageTotal;
    }

    /**
     * 加载行政人员分页数据
     * */
    private List<Manager> getAdminStaffMapperDataBySearch(AdminStaffQuery query){
        Integer currentPage = query.getCurrentPage();
        Integer startIndex = (currentPage -1 )*query.getPageSize();
        query.setStartIndex(startIndex);
        return managerMapper.getAdminStaffMapperDataBySearch(query);
    }

    /**
     * 分页功能
     * 装载行政人员分页数据
     * */
    public Response getAdminStaffPagerDataBySearch(AdminStaffQuery query){
        PagerData pagerData = new PagerData();
        pagerData.setTotal(getAdminStaffCountBySearch(query));
        pagerData.setPageTotal(getAdminStaffPageTotalBySearch(query));
        pagerData.setData(getAdminStaffMapperDataBySearch(query));
        return Response.success(pagerData);
    }
}
